#include "kernel_operator.h"
#include <type_traits>
constexpr int32_t BUFFER_NUM = 2; // tensor num for each queue

class KernelDiv {
public:
    __aicore__ inline KernelDiv() {}
    __aicore__ inline void Init(GM_ADDR x1, GM_ADDR x2, GM_ADDR y, uint32_t input_data_length, uint32_t x1_length, uint32_t x2_length, uint32_t total_length, uint32_t ALIGN_NUM, uint32_t block_size, uint32_t core_size, uint32_t core_remain)
    {
        ASSERT(GetBlockNum() != 0 && "block dim can not be zero!");
        this->x1Length = x1_length;
        this->x2Length = x2_length;
        this->blockLength = core_size + (GetBlockNum() == GetBlockIdx() + 1 ? core_remain : 0);
        this->tileLength = block_size;
        this->blockLength = this->blockLength + (this->blockLength % ALIGN_NUM ? ALIGN_NUM - this->blockLength % ALIGN_NUM : 0);
        this->startPointer = core_size * GetBlockIdx();

        xGm.SetGlobalBuffer((__gm__ DTYPE_X1 *)x1 + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
        yGm.SetGlobalBuffer((__gm__ DTYPE_X2 *)x2 + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
        zGm.SetGlobalBuffer((__gm__ DTYPE_Y *)y + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileLength * sizeof(DTYPE_X1));
        pipe.InitBuffer(inQueueY, BUFFER_NUM, this->tileLength * sizeof(DTYPE_X2));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, this->tileLength * sizeof(DTYPE_Y));
        pipe.InitBuffer(tmpBuffer1, this->tileLength * sizeof(half));
        pipe.InitBuffer(tmpBuffer2, this->tileLength * sizeof(half));
    }
    __aicore__ inline void Process()
    {
        int32_t loopCount = this->blockLength / this->tileLength + (this->blockLength % this->tileLength > 0);
        for (int32_t i = 0; i < loopCount-1; i++) {
            uint32_t position = startPointer + i * this->tileLength;
            CopyIn(position, this->tileLength);
            Compute(this->tileLength);
            CopyOut(position, this->tileLength);
        }
        uint32_t position = startPointer + (loopCount - 1) * this->tileLength;
        uint32_t length = this->blockLength - this->tileLength * (loopCount - 1);
        CopyIn(position, length);
        Compute(length);
        CopyOut(position, length);
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
        AscendC::LocalTensor<DTYPE_X1> xLocal = inQueueX.AllocTensor<DTYPE_X1>();
        AscendC::LocalTensor<DTYPE_X2> yLocal = inQueueY.AllocTensor<DTYPE_X2>();
        AscendC::DataCopy(xLocal, xGm[progress * this->tileLength], this->tileLength);
        AscendC::DataCopy(yLocal, yGm[progress * this->tileLength], this->tileLength);
        inQueueX.EnQue(xLocal);
        inQueueY.EnQue(yLocal);
    }
    __aicore__ inline void Compute(int32_t progress)
    {
        AscendC::LocalTensor<DTYPE_X1> xLocal = inQueueX.DeQue<DTYPE_X1>();
        AscendC::LocalTensor<DTYPE_X2> yLocal = inQueueY.DeQue<DTYPE_X2>();
        AscendC::LocalTensor<DTYPE_Y> zLocal = outQueueZ.AllocTensor<DTYPE_Y>();
        // 多类型支持
        if constexpr(std::is_same_v<DTYPE_Y, int8_t> || std::is_same_v<DTYPE_Y, signed char> || std::is_same_v<DTYPE_Y, int32_t>) {
            auto p1 = tmpBuffer1.Get<half>();
            auto p2 = tmpBuffer2.Get<half>();
            Cast(p1, xLocal, AscendC::RoundMode::CAST_NONE, this->tileLength);
            Cast(p2, yLocal, AscendC::RoundMode::CAST_NONE, this->tileLength);
            AscendC::Div(p1, p1, p2, this->tileLength);
            Cast(zLocal, p1, AscendC::RoundMode::CAST_NONE, this->tileLength);
        } else {
            AscendC::Div(zLocal, xLocal, yLocal, this->tileLength);
        }
        
        outQueueZ.EnQue<DTYPE_Y>(zLocal);
        inQueueX.FreeTensor(xLocal);
        inQueueY.FreeTensor(yLocal);
    }
    __aicore__ inline void CopyOut(int32_t progress)
    {
        AscendC::LocalTensor<DTYPE_Y> zLocal = outQueueZ.DeQue<DTYPE_Y>();
        AscendC::DataCopy(zGm[progress * this->tileLength], zLocal, this->tileLength);
        outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::QuePosition::VECIN, BUFFER_NUM> inQueueX, inQueueY;
    AscendC::TQue<AscendC::QuePosition::VECOUT, BUFFER_NUM> outQueueZ;
    AscendC::TBuf<AscendC::QuePosition::VECCALC> tmpBuffer1, tmpBuffer2;
    AscendC::GlobalTensor<DTYPE_X1> xGm;
    AscendC::GlobalTensor<DTYPE_X2> yGm;
    AscendC::GlobalTensor<DTYPE_Y> zGm;
    uint32_t blockLength;
    uint32_t tileLength;
    uint32_t startPointer;
    uint32_t inputdataLength;
    uint32_t x1Length;
    uint32_t x2Length;
};

extern "C" __global__ __aicore__ void div(GM_ADDR x1, GM_ADDR x2, GM_ADDR y, GM_ADDR workspace, GM_ADDR tiling) {
    GET_TILING_DATA(tiling_data, tiling);
    KernelDiv op;
    op.Init(x1, x2, y, tiling_data.totalLength, tiling_data.alignNum, tiling_data.blockSize, tiling_data.coreSize, tiling_data.coreRemain);
    op.Process();
}